package com.gadgets.cm01.role.service.impl;

import com.gadgets.cm01.role.dto.UserRoleAllocationDTO;
import com.gadgets.cm01.role.dto.UserRoleQueryDTO;
import com.gadgets.cm01.role.pojo.Role;
import com.gadgets.cm01.role.pojo.UserRoleRelation;
import com.gadgets.cm01.role.repository.IUserRoleRelationMapper;
import com.gadgets.cm01.role.service.IRoleUserAllocationService;
import com.gadgets.cm01.user.pojo.User;
import com.gadgets.framework.common.dto.Pagination;
import com.gadgets.framework.common.exception.GadgetsServiceException;
import com.gadgets.framework.common.util.PaginationUtil;
import com.gadgets.framework.common.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author UncleXiu
 * @version V1.0.0
 * @data 2020-10-15
 *
 */
@Service
public class RoleUserAllocationServiceImpl implements IRoleUserAllocationService {

    @Autowired
    private IUserRoleRelationMapper userRoleRelationMapper;

    /**
     * 角色分配
     *
     * @param allocationDTO 分配信息
     *
     * @throws GadgetsServiceException 异常信息
     */
    @Transactional(transactionManager = "gadgetsDbTransactionManage", rollbackFor = Throwable.class)
    @Override
    public void allocationRole(UserRoleAllocationDTO allocationDTO) throws GadgetsServiceException {
        Long[] roleIds = StringUtils.splitStrToLong(allocationDTO.getRoleIds());
        for (Long roleId : roleIds) {
            UserRoleRelation relation = new UserRoleRelation();
            relation.setRoleId(roleId);
            relation.setUserId(allocationDTO.getUserId());
            userRoleRelationMapper.insertUserRoleRelationByBatch(relation);
        }
    }

    /**
     * 角色收回
     *
     * @param allocationDTO 收回信息
     *
     * @throws GadgetsServiceException 异常信息
     */
    @Transactional(transactionManager = "gadgetsDbTransactionManage", rollbackFor = Throwable.class)
    @Override
    public void reAllocationRole(UserRoleAllocationDTO allocationDTO) throws GadgetsServiceException {
        Long[] roleIds = StringUtils.splitStrToLong(allocationDTO.getRoleIds());
        allocationDTO.setRoleIdaArr(roleIds);
        userRoleRelationMapper.deleteUserRoleRelationByBatch(allocationDTO);
    }

    /**
     * 查询用户-角色映射关系
     *
     * @param queryDTO   查询条件
     * @param pagination 分页条件
     *
     * @return 查询结果
     *
     * @throws GadgetsServiceException 异常信息
     */
    @Transactional(transactionManager = "gadgetsDbTransactionManage", rollbackFor = Throwable.class, propagation = Propagation.SUPPORTS)
    @Override
    public List<UserRoleRelation> queryUserRoleRelationByCondition(UserRoleQueryDTO queryDTO, Pagination pagination) throws GadgetsServiceException {
        PaginationUtil.bulidPaginationInfo(pagination);
        List<UserRoleRelation> list = userRoleRelationMapper.selectUserRoleInfoByCondition(queryDTO);
        PaginationUtil.getPaginationResultInfo(list,pagination);
        return list;
    }

    /**
     * 条件查询指定用户的角色信息
     *
     * @param queryDTO   查询条件
     * @param pagination 分页条件
     *
     * @return 角色信息
     *
     * @throws GadgetsServiceException 异常信息
     */
    @Transactional(transactionManager = "gadgetsDbTransactionManage", rollbackFor = Throwable.class, propagation = Propagation.SUPPORTS)
    @Override
    public List<Role> queryRoleInfoByCondition(UserRoleQueryDTO queryDTO, Pagination pagination) throws GadgetsServiceException {
        PaginationUtil.bulidPaginationInfo(pagination);
        List<Role> list = userRoleRelationMapper.selectRelationRoleInfoByCondition(queryDTO);
        PaginationUtil.getPaginationResultInfo(list,pagination);
        return list;
    }

    /**
     * 条件查询指定角色的用户信息
     *
     * @param queryDTO   查询条件
     * @param pagination 分页条件
     *
     * @return 查询结果
     *
     * @throws GadgetsServiceException 异常信息
     */
    @Transactional(transactionManager = "gadgetsDbTransactionManage", rollbackFor = Throwable.class, propagation = Propagation.SUPPORTS)
    @Override
    public List<User> queryUserInfoByCondition(UserRoleQueryDTO queryDTO, Pagination pagination) throws GadgetsServiceException {
        PaginationUtil.bulidPaginationInfo(pagination);
        List<User> list = userRoleRelationMapper.selectRelationUserInfoByCondition(queryDTO);
        PaginationUtil.getPaginationResultInfo(list,pagination);
        return list;
    }

}
